<template>
  <div class="system-role-container layout-padding">
    <div class="system-role-padding layout-padding-auto layout-padding-view">
      <div class="system-user-search mb15">
        <el-input v-model="state.param.keyword" size="default" placeholder="请输入公告名称" style="max-width: 180px">
        </el-input>
        <el-button size="default" type="primary" class="ml10" @click="query_user">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="primary" @click="onOpenaddAnnounce">新增</el-button>
      </div>
      <el-table :data="state.tableData" v-loading="state.loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="title" label="公告名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="content" label="公告内容" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" type="primary" @click="onOpenEditUser(scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="onUserDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
        :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.param.pageNum" background
        v-model:page-size="state.param.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
      </el-pagination>
    </div>
    <DialogComponent ref="DialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { getNoticeList, removeUser } from '/@/api/announce';
// 引入组件
const DialogComponent = defineAsyncComponent(() => import('/@/views/announce/DialogComponent.vue'));

// 定义变量内容
const DialogRef = ref();
const state = reactive({
  tableData: [
    {
      id: 1,
      title: '公告111',
      content: "内容"
    }
  ],
  total: 0,
  loading: false,
  param: {
    keyword: '',
    pageNum: 1,
    pageSize: 10,
    key: 'admin'
  },
});
// 初始化表格数据
const getTableData = () => {
  state.loading = true;
  getNoticeList(state.param).then((res: any) => {
    state.tableData = res.data
    state.total = res.total
  }).finally(() => {
    state.loading = false
  })
};
// 查询按钮
const query_user = () => {
  state.param.pageNum = 1
  getTableData()
};
// 打开修改公告弹窗
const onOpenEditUser = (row: Object) => {
  DialogRef.value.openDialog(JSON.parse(JSON.stringify(row)), 'edit');
};
// 新增公告
const onOpenaddAnnounce = () => {
  DialogRef.value.openDialog('', 'add');
}
// 删除公告
const onUserDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除“${row.username}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      removeUser({ id: row.id }).then(() => {
        getTableData();
        ElMessage.success('删除成功');
      })
    })
    .catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.param.pageSize = val;
  state.param.pageNum = 1
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
  .system-role-padding {
    padding: 15px;

    .el-table {
      flex: 1;
    }
  }
}
</style>
